package com.project.fortuneteller.context;

import com.project.fortuneteller.pojo.UserInfo;

public class UserContext {

    private static final ThreadLocal<UserInfo> USER_INFO = new ThreadLocal<>();

    public static void setUserInfo(UserInfo userInfo) {
        USER_INFO.set(userInfo);
    }


    public static UserInfo getUserInfo() {
        if (USER_INFO.get() == null) {
            synchronized (UserContext.class) {
                if (USER_INFO.get() == null) {
                    setUserInfo(UserInfo.builder().build());
                }
            }
        }
        return USER_INFO.get();
    }


    public static void remove() {
        USER_INFO.remove();
    }

    /**
     * 获取用户id
     *
     * @return {@link String}
     */
    public static String getUserId() {
        return getUserInfo().getId();
    }

    /**
     * 获取用户名
     *
     * @return {@link String}
     */
    public static String getUserName() {
        return getUserInfo().getUsername();
    }



//
//    /**
//     * 设置当前线程操作者为系统
//     */
//    public static void asSystem() {
//        setUserInfo(User.system());
//    }
}
